<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="./jquery.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery("form[name=form1] input[name=txtPhotoName]").blur(function(){
                    jQuery.ajax({
                        type:"GET",
                        data:"key="+$("form[name=form1] input[name=txtPhotoName]").val(),
                        url:"check_exist.php?type=3",
                        success:function(result){
                            if(result)
                                $("#spAvailability").html("* Photo name already exist.");
                            else
                                $("#spAvailability").html("* Photo name available.");
                        }
                    });
                });
            });
            jQuery(document).ready(function(){
                jQuery("form[name=form1] input[name=btnCreate]").click(function(){
                    jQuery.ajax({
                        type:"GET",
                        data:"key="+$("form[name=form1] input[name=txtPhotoName]").val(),
                        url:"check_exist.php?type=3",
                        success:function(result){
                            if(result)
                                var res = confirm("Photo name already exist. Would you like update it?");
                            if(!res)
                                return;
                        }
                    });
                });
            });
            
        </script>
    </head>
    <body>
        <?php
        require 'connect.inc';
        require 'session.inc';
        $res = "";
        $state = 0;
        $result;
        $res = "";
        if ($_SESSION["isLogin"] == 0)
            header("Location: login.php");
        else {
            $iduser = $_SESSION['iduser'];
            $idAlbum = $_REQUEST['album_id'];
            $idUser = $_SESSION["iduser"];
            if (isset($_POST["btnCreate"])) {
                $name = $_REQUEST["txtPhotoName"];
                $image = "";
                $ext = "";
                $types = array('image/jpeg', 'image/gif', 'image/pjpeg');
                if (count($_FILES) > 0) {
                    $file = $_FILES['fImage'];
                    if (!in_array($file['type'], $types)) {
                        return;
                    }
                    if ($file['error'] == 0) {
                        $image = $file['name'];
                        $temp = pathinfo($file["name"]);
                        $ext = $temp['extension'];
                    }
                }
                $strSQL = "SELECT * FROM photo WHERE photo_name = '$name'";
                $result = mysql_query($strSQL);
                if ($result && mysql_num_rows($result) > 0) {
                    while ($row = mysql_fetch_array($result)) {
                        $idphoto = $row["photo_id"];
                        move_uploaded_file($file['tmp_name'], "upload/$idUser/$idAlbum/$idphoto.$ext");
                    }
                } else {
                    $strSQL = "INSERT INTO `photo` (`album_id`, `photo_name`, `date_created`, `date_modified`, `image`)
                                            VALUES ($idAlbum, '$name', now(), now(), '$image')";
                    //echo $strSQL;
                    $result = mysql_query($strSQL);
                    $idphoto = mysql_insert_id();
                    //Create user's folder and upload file
                    if ($result) {
                        mysql_query("UPDATE PHOTO SET image = '$idphoto.$ext' WHERE photo_id = $idphoto");
                        move_uploaded_file($file['tmp_name'], "upload/$idUser/$idAlbum/$idphoto.$ext");
                    }
                }
            }
            $res .=
                    "<table width='100%' border='1' cellspacing='0'>
            <tbody>
                <tr></tr>
                <tr>
                    <td>
                        <img src='logo.jpg'>
                    </td>
                    <td>
                        <h1>Photo Album Website</h1>
                    </td>
                </tr>
                <tr>
                    <td width='20%' valign='top'>
                        <a href='index.php'>Home</a>
                        <br>
                        <a href='my_albums.php'>My Albums</a>
                        <br>
                        <a href='users.php'>Other persons</a>
                    </td><td>
                    <table>
                        <tbody>
                            <tr>
                                <td rowspan='5'>";

            $strSQL = "SELECT * FROM `album` a
                        WHERE album_id = $idAlbum and public = 1";
            $result = mysql_query($strSQL);
            if ($result)
                while ($row = mysql_fetch_array($result)) {
                    $owner = $row['owner'];

                    $avatar = $row['avatar'];
                    $path = "./upload/$owner/$idAlbum/avatar/$avatar";
                    $res .= "<img src='$path' hieght ='200'>";
                    $fullname = "";
                    $r = mysql_query("SELECT * FROM Users where user_id = $idUser");
                    while ($ro = mysql_fetch_array($r)) {
                        $fullname .= $ro['full_name'];
                    }
                    $name = $row['album_name'];
                    $dc = $row['date_created'];
                    $dm = $row['date_modified'];
                    $pl = $row['public'];
                    $res .= "</td>
                            <td>$name</td>
                        </tr>
                        <tr>
                            <td>$fullname</td>
                        </tr>
                        <tr>
                            <td>$dc</td>
                        </tr>
                        <tr>
                            <td>$dm</td>
                        </tr>
                        <tr>
                            <td>Public: $pl</td>
                        </tr>
                    ";
                    break;
                }
            $res.=" </tbody>
                </table>
                <br/><hr>
                        <center>
                        <h2>Add Photo</h2>
                    </center>
                   <form enctype='multipart/form-data' method='post' action='edit_album.php?album_id=$idAlbum' name='form1'>
                    <table>
                        <tbody>
                            <tr>
                                <td>

                                </td>
                            </tr>
                            <tr>
                                <td>Photo name: </td>
                                <td>
                                    <input type='text' value='' name='txtPhotoName'>
                                    <span id='spAvailability' style='color: red'>
                                        <font style='color:red'></font>
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td>Image: </td>
                                <td>
                                    <input type='file' value='' name='fImage'>
                                </td>
                            </tr>
                            <tr>
                            <td></td>
                                <td>
                                    <input type='submit' value='Create' name='btnCreate'>
                                    <input type='reset' name='btnCancel' value='Cancel'>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
                        <br/><hr>
                        My albums:
                     <br>";
            $strSQL = "SELECT * FROM `photo`
                    WHERE album_id = $idAlbum";
            //echo $strSQL;
            $result = mysql_query($strSQL);
            if ($result) {
                if (mysql_num_rows($result) < 1)
                    $res .= "Album current empty!";
                else
                    while ($row = mysql_fetch_array($result)) {
                        $idPhoto = $row["album_id"];
                        $image = $row["image"];
                        $name = $row["photo_name"];
                        $res .= "<table style='float:left;'>
                        <tbody>
                            <tr>
                                <td>
                                    <img height='100' src='./upload/$iduser/$idAlbum/$image'>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <a href='# '>$name</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>";
                    }
            }
            // User logged in

            $res .= " <td width='20%' valign='top'>
                Wellcom <a href='#'>" . $_SESSION["username"] . "</a>!
                |
                <a href='logout.php'>Logout</a></td>
            </tr>
        </tbody>
        </table>";

            echo $res;
        }
        ?>
    </body>


</html>
